#include <bits/stdc++.h>
// 2025/05/17
// tag: Persistent segment tree
// Author: Forever
using namespace std;
using int64 = int64_t;

#define MAXN 65536
#define LOG_MAX 16

int n, m, in[MAXN], ans[MAXN];
vector<int> tree[MAXN], tree2[MAXN], tree3[MAXN];

int up[MAXN][LOG_MAX];
int depth[MAXN];

void preprocess(int u, int parent)
{
    up[u][0] = parent, depth[u] = depth[parent] + 1;
    for (int k = 1; k < LOG_MAX; ++k) 
        up[u][k] = up[up[u][k - 1]][k - 1];
}

int lca(int u, int v)
{
    if (depth[u] < depth[v])
        swap(u, v);
    for (int k = LOG_MAX - 1; k >= 0; --k) 
        if (depth[u] - (1 << k) >= depth[v]) 
            u = up[u][k];
    if (u == v)
        return u;
    for (int k = LOG_MAX - 1; k >= 0; --k) 
        if (up[u][k] != up[v][k]) 
            u = up[u][k], v = up[v][k];
    return up[u][0];
}

void dfs(int u)
{
    ans[u] = 1;
    for (int v : tree3[u]) {
        dfs(v);
        ans[u] += ans[v];
    }
}

void topo(){
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (in[i] == 0)
            q.push(i);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        int l = tree2[u][0];
        for (int j = 1; j < tree2[u].size(); j++)
            l = lca(l, tree2[u][j]);
        tree3[l].push_back(u);
        cout << u << " " << l << endl;
        preprocess(u, l);
        for (int v : tree[u]) {
            in[v]--;
            if (in[v] == 0)
                q.push(v);
        }
    }
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int x;
        while (cin >> x && x != 0) {
            tree[x].push_back(i), tree2[i].push_back(x);
            in[i]++;
        }
    }
    for (int i = 1; i <= n; i++)
        if (in[i] == 0)
            tree[0].push_back(i), tree2[i].push_back(0);
    topo();
    dfs(0);
    for (int i = 1; i <= n; i++)
        cout << ans[i] - 1 << endl;
    return 0;
}